import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_program/HomePage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_program/fragment_five/FiveFramgent.dart';
import 'package:flutter_program/fragment_four/FourFramgent.dart';
import 'package:flutter_program/fragment_four/bean/SheBaiListBean.dart';
import 'package:flutter_program/fragment_one/OrderPagerFragment.dart';
import 'package:flutter_program/fragment_second/SencondFramgent.dart';
import 'package:flutter_program/fragment_six/SixFramgent.dart';
import 'package:flutter_program/fragment_thread/ThreadFramgent.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<SheBaiListBean> mDataFouDate = new List();
  var tabImages;
  var appBarTitles = ['统计', '工单', '管理设备', '管理设备', '计划', '我的'];
  BuildContext context;
  int _currentPageIndex = 0;
  GlobalKey _myKey = new GlobalKey();
  var _pageController = new PageController(initialPage: 0);

  List _pageList=new List();

  Image getTabIcon(int curIndex) {
    if (curIndex == _currentPageIndex) {
      return tabImages[curIndex][1];
    }
    return tabImages[curIndex][0];
  }

  Text getTabTitle(int curIndex) {
    if (curIndex == _currentPageIndex) {
      return new Text(appBarTitles[curIndex],
          style: new TextStyle(fontSize: 13.0, color: const Color(0xff1296db)));
    } else {
      return new Text(appBarTitles[curIndex],
          style: new TextStyle(fontSize: 13.0, color: Colors.grey));
    }
  }

  Image getTabImage(path) {
    return new Image.asset(path, width: 24.0, height: 24.0);
  }

  void _pageChange(int index) {
    setState(() {
      if (_currentPageIndex != index) {
        _currentPageIndex = index;
      }
    });
  }

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  _loadData() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    try {
      var dio = new Dio();
      dio.options.baseUrl = "http://118.25.173.88:8080";
      dio.options.connectTimeout = 5000;
      dio.options.receiveTimeout = 5000;
      //dio.options.headers["JSESSIONID"]= preferences.getString("JSESSIONID");
      dio.interceptor.request.onSend = (Options options) {
        print('cookiess:=' + preferences.getString("Cookie"));
        options.headers["Cookie"] = preferences.getString("Cookie");
        return options;
      };

      var response =
          await dio.post("/tpm//workorder/repairsheetlist/getOfflineData");
      int count1 = response.data['sparePartEntities'].length;
      print("数量:" + count1.toString());
      setState(() {
        for (var i = 0; i < count1; i++) {
          SheBaiListBean already = new SheBaiListBean(
              code: response.data['sparePartEntities'][i]["code"].toString(),
            createBy: response.data['sparePartEntities'][i]["createBy"].toString(),
            createDate: response.data['sparePartEntities'][i]["createDate"].toString(),
            currRepertory: response.data['sparePartEntities'][i]["currRepertory"],
            delFlag: response.data['sparePartEntities'][i]["delFlag"],
            deptId: response.data['sparePartEntities'][i]["deptId"].toString(),
            deptName: response.data['sparePartEntities'][i]["deptName"].toString(),
            equipmentId: response.data['sparePartEntities'][i]["equipmentId"].toString(),
            equipmentList: response.data['sparePartEntities'][i]["equipmentList"],
            equipmentName: response.data['sparePartEntities'][i]["equipmentName"].toString(),
            equipmentTypeIds: response.data['sparePartEntities'][i]["equipmentTypeIds"],
            equipmentTypeList: response.data['sparePartEntities'][i]["equipmentTypeList"],
            equipmentTypeNames: response.data['sparePartEntities'][i]["equipmentTypeNames"].toString(),
            fileId: response.data['sparePartEntities'][i]["fileId"],
            firmId: response.data['sparePartEntities'][i]["firmId"],
            firmInfoList: response.data['sparePartEntities'][i]["firmInfoList"],
            firmName: response.data['sparePartEntities'][i]["firmName"],
            money: response.data['sparePartEntities'][i]["money"],
            name: response.data['sparePartEntities'][i]["name"],
            number: response.data['sparePartEntities'][i]["number"],
            quantity: response.data['sparePartEntities'][i]["quantity"],
            repertoryMax: response.data['sparePartEntities'][i]["repertoryMax"],
            repertoryMin: response.data['sparePartEntities'][i]["repertoryMin"],
            sparePartId: response.data['sparePartEntities'][i]["sparePartId"],
            specModels: response.data['sparePartEntities'][i]["specModels"],
            systemFileList: response.data['sparePartEntities'][i]["systemFileList"],
            type: response.data['sparePartEntities'][i]["type"],
            typeName: response.data['sparePartEntities'][i]["typeName"],
            updateDate: response.data['sparePartEntities'][i]["updateDate"],
            unitPrice: response.data['sparePartEntities'][i]["unitPrice"],);
          print("wodeai" + already.code.toString());
          print("我的1" + response.data['sparePartEntities'][i]["code"].toString());
          print("我的2" + response.data['sparePartEntities'][i]["unitPrice"].toString());
          mDataFouDate.add(already);
        }
        print("wwwwww" + response.data['sparePartEntities'][1]["unitPrice"].toString());

        _pageList = new List();
        _pageList.add(new OrderPagerFragment("统计")); //工单
        _pageList.add(new SencondFragment("工单"));
        _pageList.add(new ThreadFramgent("设备管理",mDataFouDate));
        _pageList.add(new FourFramgent("设备检查", mDataFouDate));
        _pageList.add(new FiveFramgent("计划"));
        _pageList.add(new SixFragment("我的"));
      });
      /*setState(() {
        for (var i = 0; i < count1; i++) {
          SheBaiListBean alreadyAplayBean1 = new SheBaiListBean(
            code: response.data['sparePartEntities'][i]["code"].toString(),
            */ /*code: response.data['sparePartEntities'][i]["code"].toString(),
            createBy: response.data['sparePartEntities'][i]["createBy"].toString(),
            createDate: response.data['sparePartEntities'][i]["createDate"].toString(),
            currRepertory: response.data['sparePartEntities'][i]["currRepertory"],
            delFlag: response.data['sparePartEntities'][i]["delFlag"],
            deptId: response.data['sparePartEntities'][i]["deptId"].toString(),
            deptName: response.data['sparePartEntities'][i]["deptName"].toString(),
            equipmentId: response.data['sparePartEntities'][i]["equipmentId"].toString(),
            equipmentList: response.data['sparePartEntities'][i]["equipmentList"],
            equipmentName: response.data['sparePartEntities'][i]["equipmentName"].toString(),
            equipmentTypeIds: response.data['sparePartEntities'][i]["equipmentTypeIds"],
            equipmentTypeList: response.data['sparePartEntities'][i]["equipmentTypeList"],
            equipmentTypeNames: response.data['sparePartEntities'][i]["equipmentTypeNames"].toString(),
            fileId: response.data['sparePartEntities'][i]["fileId"],
            firmId: response.data['sparePartEntities'][i]["firmId"],
            firmInfoList: response.data['sparePartEntities'][i]["firmInfoList"],
            firmName: response.data['sparePartEntities'][i]["firmName"],
            money: response.data['sparePartEntities'][i]["money"],
            name: response.data['sparePartEntities'][i]["name"],
            number: response.data['sparePartEntities'][i]["number"],
            quantity: response.data['sparePartEntities'][i]["quantity"],
            repertoryMax: response.data['sparePartEntities'][i]["repertoryMax"],
            repertoryMin: response.data['sparePartEntities'][i]["repertoryMin"],
            sparePartId: response.data['sparePartEntities'][i]["sparePartId"],
            specModels: response.data['sparePartEntities'][i]["specModels"],
            systemFileList: response.data['sparePartEntities'][i]["systemFileList"],
            type: response.data['sparePartEntities'][i]["type"],
            typeName: response.data['sparePartEntities'][i]["typeName"],
            updateDate: response.data['sparePartEntities'][i]["updateDate"],
            unitPrice: response.data['sparePartEntities'][i]["unitPrice"],*/ /*
          );
          print("不会吧？:"+mDataFouDate[i].code.toString());

          mDataFouDate.add(alreadyAplayBean1);
          print("wangxiaoj"+mDataFouDate.toString());

        }
       print("wangxiaoj"+mDataFouDate.toString());
      });*/
    } catch (e) {
      print(e.toString());
    }
  }

  void initData() {
    tabImages = [
      [
        getTabImage('images/mipmap-hdpi-v4/gteq_icon_statistics_normal.png'),
        getTabImage('images/mipmap-hdpi-v4/gteq_icon_statistics_pressed.png')
      ],
      [
        getTabImage('images/mipmap-hdpi-v4/gteq_icon_order_normal.png'),
        getTabImage('images/mipmap-hdpi-v4/gteq_icon_order_pressed.png')
      ],
      [
        getTabImage('images/mipmap-hdpi-v4/gteq_icon_eq_normal.png'),
        getTabImage('images/mipmap-hdpi-v4/gteq_icon_eq_pressed.png')
      ],
      [
        getTabImage('images/mipmap-hdpi-v4/gteq_icon_pj_normal.png'),
        getTabImage('images/mipmap-hdpi-v4/gteq_icon_pj_pressed.png')
      ],
      [
        getTabImage('images/mipmap-hdpi-v4/gteq_icon_plan_normal.png'),
        getTabImage('images/mipmap-hdpi-v4/gteq_icon_plan_pressed.png')
      ],
      [
        getTabImage('images/mipmap-hdpi-v4/gteq_icon_mine_normal.png'),
        getTabImage('images/mipmap-hdpi-v4/gteq_icon_mine_pressed.png')
      ],
    ];


  }

  _showDialog() {
    showDialog<Null>(
      context: context,
      child: new AlertDialog(
          key: _myKey,
          content: new Container(
              margin: new EdgeInsets.only(top: 40.0),
              alignment: Alignment.center,
              height: 20.0,
              width: MediaQuery.of(context).size.width,
              child: Text('是否退出登录？')),
          actions: <Widget>[
            Container(
                width: MediaQuery.of(context).size.width - 96.0,
                child: Column(
                  children: <Widget>[
                    Container(
                      width: MediaQuery.of(context).size.width - 96.0,
                      color: Colors.black12,
                      height: 1.0,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        new FlatButton(
                          onPressed: () {
                            Navigator.pop(context);
                            SystemNavigator.pop();
                          },
                          child: new Text('确定'),
                        ),
                        Container(
                          width: 1.0,
                          color: Colors.black12,
                          height: 40.0,
                        ),
                        new FlatButton(
                          onPressed: () {
                            print(_myKey.currentContext
                                .findRenderObject()
                                .semanticBounds
                                .width);
                            print(_myKey.currentContext
                                .findRenderObject()
                                .semanticBounds
                                .left);
                            print(_myKey.currentContext
                                .findRenderObject()
                                .semanticBounds
                                .right);
                            print(_myKey.currentContext
                                .findRenderObject()
                                .semanticBounds
                                .centerLeft);
                            print(_myKey.currentContext
                                .findRenderObject()
                                .semanticBounds
                                .centerRight);
                            print(
                                "semanticBounds:${_myKey.currentContext.findRenderObject().semanticBounds.size} paintBounds:${_myKey.currentContext.findRenderObject().paintBounds.size} size:${_myKey.currentContext.size}");

                            Navigator.pop(context);
                          },
                          child: Text("取消"),
                        ),
                      ],
                    ),
                  ],
                )),
          ]),
    );
  }

  Future<bool> _requestPop() {
    _showDialog();
    return new Future.value(false);
  }

  @override
  Widget build(BuildContext context) {
    this.context = context;
    initData();

    return new WillPopScope(
      onWillPop: _requestPop,
      child: Scaffold(
        appBar: new AppBar(
          automaticallyImplyLeading: false,
          actions: <Widget>[
            Padding(
              child: Image.asset(
                "images/mipmap-hdpi-v4/gteq_icon_scan.png",
                width: 28.0,
                height: 28.0,
              ),
              padding: new EdgeInsets.only(right: 13.0),
            ),
          ],
          elevation: 2.0,
          title: new Text("微感科技"),
          centerTitle: true,
        ),
        body: new PageView.builder(
          onPageChanged: _pageChange,
          controller: _pageController,
          itemBuilder: (BuildContext context, int index) {
            return _pageList[index];
          },
          itemCount: 6,
        ),
        bottomNavigationBar: new BottomNavigationBar(
          items: [
            new BottomNavigationBarItem(
                icon: getTabIcon(0), title: getTabTitle(0)),
            new BottomNavigationBarItem(
                icon: getTabIcon(1), title: getTabTitle(1)),
            new BottomNavigationBarItem(
                icon: getTabIcon(2), title: getTabTitle(2)),
            new BottomNavigationBarItem(
                icon: getTabIcon(3), title: getTabTitle(3)),
            new BottomNavigationBarItem(
                icon: getTabIcon(4), title: getTabTitle(4)),
            new BottomNavigationBarItem(
                icon: getTabIcon(5), title: getTabTitle(5)),
          ],
          type: BottomNavigationBarType.fixed,
          currentIndex: _currentPageIndex,
          onTap: (int index) {
            //_pageController.jumpToPage(index); 没有动画的页面切换
            _pageController.animateToPage(index,
                duration: new Duration(milliseconds: 500),
                curve: new ElasticOutCurve(0.8));
            _pageChange(index);
          },
        ),
      ),
    );
  }
}
